package com.sdp.right.db.sys_org.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.right.db.sys_org.mapper.SysOrgMapper;
import com.sdp.right.db.sys_org.service.ISysOrgService;
import com.sdp.right.entity.SysOrg;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-17
 */
@Service
@DS(value="right")
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements ISysOrgService {

	@Override
	public List<JSONObject> queryOrgTableTree() {
		List<SysOrg> orgList = list();
		//所有顶级组织
		List<SysOrg> rootList = new ArrayList<SysOrg>();
		if(orgList!=null && orgList.size()>0) {
			for(SysOrg org : orgList) {
				if(StringUtils.isEmpty(org.getParentNo()) || RightConstants.否.getCode().equals(org.getParentNo())) {
					
					rootList.add(org);
				}
			}
		}
		
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		if(rootList!=null && rootList.size()>0) {
			JSONObject parentObj =null;
			for(int i=0;i<rootList.size();i++) {
				parentObj = JSON.parseObject(JSON.toJSONString(rootList.get(i),SerializerFeature.WriteMapNullValue).toString());
				treeList.add(parentObj);
				getChildOrg(parentObj, orgList);
			}
		}
		
		return treeList;
	}

	
	@SuppressWarnings("unchecked")
	private void getChildOrg(JSONObject parentObj,List<SysOrg> orgList) {
		List<Object> childList = new ArrayList<Object>();
		for(int j=0;j<orgList.size();j++) {
			if(!StringUtils.isEmpty(orgList.get(j).getParentNo()) && parentObj.getString("orgNo").equals(orgList.get(j).getParentNo())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				JSONObject childObj = JSON.parseObject(JSON.toJSONString(orgList.get(j),SerializerFeature.WriteMapNullValue).toString());
				childList.add(childObj);
				getChildOrg(childObj, orgList);
			}
			
		}
	}
	
	
	/**
	 * 组织下拉树
	 * @return
	 */
	public List<JSONObject> queryOrgTreeSelect(){
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		List<SysOrg> orgList = list();
		List<SysOrg> rootList = new ArrayList<SysOrg>();
		List<String> rootKeys = new ArrayList<String>();
		if(orgList!=null && orgList.size()>0) {
			for(SysOrg org : orgList) {
				if(StringUtils.isEmpty(org.getParentNo()) || RightConstants.否.getCode().equals(org.getParentNo())) {
					rootList.add(org);
					rootKeys.add(org.getKeyId());
				}
			}
		}
		
		if(rootList!=null && rootList.size()>0) {
			
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = new JSONObject();
				parentObj.put("title", rootList.get(i).getOrgName());
				parentObj.put("value", rootList.get(i).getOrgNo());
				parentObj.put("key",rootList.get(i).getKeyId());
				treeList.add(parentObj);
				getChildOrgTreeSelect(parentObj, orgList);
			}
		}
		
		return treeList;
	}
	
	@SuppressWarnings({ "unchecked" })
	private void getChildOrgTreeSelect(JSONObject parentObj,List<SysOrg> orgList) {
		List<Object> childList = new ArrayList<Object>();
	
		for(int j=0;j<orgList.size();j++) {
			if(!StringUtils.isEmpty(orgList.get(j).getParentNo()) && parentObj.getString("value").equals(orgList.get(j).getParentNo())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				
				JSONObject childObj = new JSONObject();
				childObj.put("title", orgList.get(j).getOrgName());
				childObj.put("value", orgList.get(j).getOrgNo());
				childObj.put("key",orgList.get(j).getKeyId());
				childList.add(childObj);
				getChildOrgTreeSelect(childObj, orgList);
			}
			
		}
	}


	@Override
	public SysOrg querySysOrgByNo(String orgNo) {
		LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysOrg::getOrgNo, orgNo);
		return getOne(queryWrapper,false);
	}


	@Override
	public void removeSysOrgByNo(String orgNo) {
		LambdaQueryWrapper<SysOrg> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(SysOrg::getOrgLevel, "["+orgNo+"]");
		remove(queryWrapper);
		
	}
	
	
}
